/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard. All rights reserved. This program and the accompanying materials are made available under the terms of the GNU Lesser
 * Public License v2.1 which accompanies this distribution, and is available at http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors: Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.resources.tomcat;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.IllegalBindingException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;
import org.objectweb.fractal.api.control.LifeCycleController;

import fr.jade.base.JadeBaseComponent;
import fr.jade.fraclite.api.control.NoSuchAttributeException;
import fr.jade.resources.api.JdbcConnector;
import fr.jade.resources.api.JkConnector;
import fr.jade.resources.api.Servlets;
import fr.jade.util.JadeException;
import fr.jade.util.ShellCommand;

public class TomcatWrapper extends JadeBaseComponent implements JkConnector, LifeCycleController {

  // Attributes
  protected static final String   tomcatBaseAttName    = "tomcatBase";
  protected String                tomcatBase;
  
  protected static final String   portAttName          = "port";
  protected int                   port;

  protected static final String[] listAtt              = { portAttName, tomcatBaseAttName };

  // Bindings
  protected static final String   webAppItfName        = "webapp";
  protected Servlets              webApp;

  protected static final String   jdbcConnectorItfName = "jdbc";
  protected JdbcConnector         jdbc;

  protected static final String[] listItf              = { webAppItfName, jdbcConnectorItfName };

  boolean started = false;
  
  public TomcatWrapper() {
  }

  @Override
  public String getFcState() {
    return started ? STARTED : STOPPED;
  }

  @Override
  public void startFc() throws IllegalLifeCycleException {
    if (!started) {
      started = true;
      try {
        String[] env = new String[2];
        env[0] = "TOMCAT_HOME=" + tomcatBase;
        env[1] = "JAVA_HOME=/home/ssicard/local/jdk1.6.0";
        
        ShellCommand.syncExec(tomcatBase+"/bin/startup.sh", env);

      } catch (JadeException e) {
        e.printStackTrace();
      }
    }
  }

  @Override
  public void stopFc() throws IllegalLifeCycleException {
    if (started) {
      started = false;
      try {
        String[] env = new String[2];
        env[0] = "TOMCAT_HOME=" + tomcatBase;
        env[1] = "JAVA_HOME=/home/ssicard/local/jdk1.6.0";
        
        ShellCommand.syncExec(tomcatBase+"/bin/shutdown.sh", env);
      } catch (JadeException e) {
        e.printStackTrace();
      }
    }
  }

  @Override
  public void bindFc(String itfName, Object itfValue) throws NoSuchInterfaceException, IllegalBindingException, IllegalLifeCycleException {
    if (itfName.equals(webAppItfName)) {
      webApp = (Servlets) itfValue;
    } else if (itfName.equals(jdbcConnectorItfName)) {
      jdbc = (JdbcConnector) itfValue;
    } else {
      super.bindFc(itfName, itfValue);
    }
  }

  @Override
  public void unbindFc(String itfName) throws NoSuchInterfaceException, IllegalBindingException, IllegalLifeCycleException {
    if (itfName.equals(webAppItfName)) {
      webApp = null;
    } else if (itfName.equals(jdbcConnectorItfName)) {
      jdbc = null;
    } else {
      super.unbindFc(itfName);
    }
  }

  @Override
  public Object lookupFc(String itfName) throws NoSuchInterfaceException {
    if (itfName.equals(webAppItfName)) {
      return webApp;
    } else if (itfName.equals(jdbcConnectorItfName)) {
      return jdbc;
    } else {
      return super.lookupFc(itfName);
    }
  }

  @Override
  public String[] listFc() {
    return super.concat(listItf, super.listFc());
  }

  @Override
  public String getAttribute(String name) throws NoSuchAttributeException {
    if (name.equals(portAttName)) {
      return Integer.toString(port);
    } else if (name.equals(tomcatBaseAttName)) {
      return tomcatBase;
    } else {
      return super.getAttribute(name);
    }
  }

  @Override
  public String[] listFcAtt() {
    return super.concat(listAtt, super.listFcAtt());
  }

  @Override
  public void setAttribute(String name, String value) throws NoSuchAttributeException {
    if (name.equals(portAttName)) {
      port = Integer.parseInt(value);
    } else if (name.equals(tomcatBaseAttName)) {
      tomcatBase = value;
    } else {
      super.setAttribute(name, value);
    }
  }

  @Override
  public int getPort() {
    return port;
  }

  @Override
  public String getHostName() {
    try {
      return InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
      return null;
    }
  }
}
